package com.huarui.saleInvoice.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.saleInvoice.model.SaleInvoice;
import com.huarui.saleInvoice.service.SaleInvoiceService;
import com.huarui.saleInvoiceItem.model.SaleInvoiceItem;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.opensymphony.xwork2.Action;

public class SaleInvoiceAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private Integer id;
	private SessionMessage sm=null;
	private Integer start;
	private Integer limit;
	@SuppressWarnings("unchecked")
	private ListModel listModel=new ListModel();
	private String sort;
	private String dir;
	private String receivableIds;
	private Integer customerId;
	private String saleInvoiceJson;
	private String saleInvoiceItemsJson;
	private SaleInvoiceService saleInvoiceService;
	private Map<String,Object> resultMap=new HashMap<String,Object>();
	public String extList() {
		sm = UserHolder.getCurrentSessionMessage();
		listModel.setPageSize(limit == null ? 10 : limit);
		listModel.setPage(start == null ? 0 : start / limit + 1);
		listModel.setOrderBy(sort==null?"id":sort);
		if (dir != null)listModel.setDesc(dir.equals("DESC") ? true : false);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("companyId", sm.getCompanyId());
		saleInvoiceService.pagingByCondition(listModel, map);
		resultMap.put("totalProperty", listModel.getTotal());
		resultMap.put("root", listModel.getResult());
		return Action.SUCCESS;
	}
	/* 从支付页面跳过来新建单据 */
	public String  createFormReceivable(){	
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=saleInvoiceService.createFormPayable(receivableIds, customerId, sm);
		return Action.SUCCESS;
	}
	public String getSaleInvoiceById(){
		sm= UserHolder.getCurrentSessionMessage();
		resultMap = saleInvoiceService.getSaleInvoiceById(id,sm);
		resultMap.put("userId", sm.getUserId());
		return Action.SUCCESS;
	}
	/* 保存单据 */
	@SuppressWarnings("unchecked")
	public String save() {
		sm=UserHolder.getCurrentSessionMessage();
		String[] dateFormats = new String[] {"yyyy-MM-dd"};
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
		SaleInvoice saleInvoice=(SaleInvoice) JSONObject.toBean(JSONObject.fromObject(saleInvoiceJson),SaleInvoice.class);
		List<SaleInvoiceItem> list=(List<SaleInvoiceItem>) JSONArray.toCollection(JSONArray.fromObject(saleInvoiceItemsJson), SaleInvoiceItem.class);
		resultMap=saleInvoiceService.saveSaleInvoice(saleInvoice, list, sm);
		return Action.SUCCESS;
	}
	/* 提交单据 */
	@SuppressWarnings("unchecked")
	public String submit() {
		sm=UserHolder.getCurrentSessionMessage();
		String[] dateFormats = new String[] {"yyyy-MM-dd"};
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats));
		resultMap=saleInvoiceService.submitSaleInvoice(saleInvoiceJson, saleInvoiceItemsJson);
		return Action.SUCCESS;
	}
	/* 删除单据 */
	public String delete() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=saleInvoiceService.delete(id,sm);
		return Action.SUCCESS;
	}
	/* 释放单据 */
	public String release() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=saleInvoiceService.release(id, sm);
		return Action.SUCCESS;
	}
	/* 接手单据 */
	public String receive() {
		sm=UserHolder.getCurrentSessionMessage();
		resultMap=saleInvoiceService.receive(id, sm);
		return Action.SUCCESS;
	}
	public Map<String, Object> getResultMap() {
		return resultMap;
	}
	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	@SuppressWarnings("unchecked")
	public void setListModel(ListModel listModel) {
		this.listModel = listModel;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public void setDir(String dir) {
		this.dir = dir;
	}
	public void setSaleInvoiceService(SaleInvoiceService saleInvoiceService) {
		this.saleInvoiceService = saleInvoiceService;
	}
	public void setReceivableIds(String receivableIds) {
		this.receivableIds = receivableIds;
	}
	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}
	public void setSaleInvoiceJson(String saleInvoiceJson) {
		this.saleInvoiceJson = saleInvoiceJson;
	}
	public void setSaleInvoiceItemsJson(String saleInvoiceItemsJson) {
		this.saleInvoiceItemsJson = saleInvoiceItemsJson;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	
}
